"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const readText_1 = __importDefault(require("../../lib/FSTool/readText"));
const { createLogger, format, transports } = require('winston');
const path_1 = __importDefault(require("path"));
const configTool_1 = __importDefault(require("../configTool"));
const writeText_1 = __importDefault(require("../../lib/FSTool/writeText"));
const { combine, timestamp, ms, printf } = format;
const sisFormat = printf(({ level, message, timestamp }) => {
    return `${new Date(timestamp).toLocaleString()} ${level} : ${message}`;
});
let logger = null;
const LogTool = {
    getLogger: async () => {
        await configTool_1.default.init();
        if (logger) {
            return logger;
        }
        else {
            logger = createLogger({
                level: 'info',
                format: combine(ms(), timestamp(), sisFormat),
                transports: [
                    new transports.File({
                        filename: path_1.default.join(configTool_1.default.appBasePath, 'temp/logs/httpServer/info.log'),
                        level: 'info',
                        format: format.json()
                    }),
                    new transports.File({
                        filename: path_1.default.join(configTool_1.default.appBasePath, 'temp/logs/httpServer/warn.log'),
                        level: 'warn',
                        format: format.json()
                    }),
                    new transports.File({
                        filename: path_1.default.join(configTool_1.default.appBasePath, 'temp/logs/httpServer/error.log'),
                        level: 'error',
                        format: format.json()
                    }),
                    new transports.Console({
                        level: 'warn',
                        format: format.combine(format.colorize(), format.splat(), sisFormat)
                    })
                ]
            });
        }
        return logger;
    },
    queryLog: async (level) => {
        await configTool_1.default.init();
        let logPath = '';
        if (level === 'info') {
            logPath = path_1.default.join(configTool_1.default.appBasePath, 'temp/logs/httpServer/info.log');
        }
        else if (level === 'warn') {
            logPath = path_1.default.join(configTool_1.default.appBasePath, 'temp/logs/httpServer/warn.log');
        }
        else if (level === 'error') {
            logPath = path_1.default.join(configTool_1.default.appBasePath, 'temp/logs/httpServer/error.log');
        }
        const logTxt = (0, readText_1.default)(logPath)?.split('\r\n').slice(0, -2);
        return JSON.parse(`[${logTxt}]`);
    },
    clearLog: async (level = 'info') => {
        await configTool_1.default.init();
        let logPath = '';
        if (level === 'info') {
            logPath = path_1.default.join(configTool_1.default.appBasePath, 'temp/logs/httpServer/info.log');
        }
        else if (level === 'warn') {
            logPath = path_1.default.join(configTool_1.default.appBasePath, 'temp/logs/httpServer/warn.log');
        }
        else if (level === 'error') {
            logPath = path_1.default.join(configTool_1.default.appBasePath, 'temp/logs/httpServer/error.log');
        }
        (0, writeText_1.default)(logPath, '');
    }
};
exports.default = LogTool;
